#include <avr/io.h>
#include <util/delay.h>

#include "hd44780_low.h"
#include "hd44780fw.h"
#include "hd44780fw_cppwrapper.hpp"

void inf1995_hd44780_init(volatile uint8_t* ddr, volatile uint8_t* port,
struct hd44780fw_conf* fw_conf, struct hd44780_l_conf* low_conf) {
	*ddr |= (_BV(7) | _BV(6) | _BV(5) | _BV(4) | _BV(3) | _BV(2) | _BV(1));
	low_conf->rs_i = 7;
	low_conf->rw_i = 6;
	low_conf->en_i = 5;
	low_conf->db7_i = 4;
	low_conf->db6_i = 3;
	low_conf->db5_i = 2;
	low_conf->db4_i = 1;
	low_conf->rs_port = low_conf->rw_port = low_conf->en_port =
		low_conf->db7_port = low_conf->db6_port = low_conf->db5_port =
		low_conf->db4_port = port;
	low_conf->line1_base_addr = 0x00;
	low_conf->line2_base_addr = 0x40;
	low_conf->dl = HD44780_L_FS_DL_4BIT;
	fw_conf->low_conf = low_conf;
	fw_conf->total_chars = 32;
	fw_conf->font = HD44780_L_FS_F_58;
	fw_conf->lines = HD44780_L_FS_N_DUAL;
}

int main(void) {
	struct hd44780fw_conf fw_conf;
	struct hd44780_l_conf low_conf;
	
	inf1995_hd44780_init(&DDRC, &PORTC, &fw_conf, &low_conf);
	hd44780 disp(&fw_conf);
	disp << "lolz!" << ' ' << 0 << -1 << '=' << 2452;
/*	
	hd44780fw_init(cf);
	_delay_ms(2000.0);
	
	hd44780fw_cat_int16(cf, -23);
	_delay_ms(2000.0);
	
	hd44780fw_set_bc_index(cf, cf->half_chars + 3);
	hd44780fw_en_blink(cf, HD44780_L_DISP_B_ON);
	_delay_ms(2000.0);
	
	hd44780fw_cat_string(cf, ".. allo!");
	_delay_ms(2000.0);
	
	hd44780fw_clear(cf);
	_delay_ms(2000.0);
	
	hd44780fw_write(cf, "coucou toi comment ca va ici??!!", 0, 0);
	_delay_ms(2000.0);
	
	uint8_t cc [] = {
		0x01,
		0x02,
		0x03,
		0x04,
		0x05,
		0x06,
		0x07,
		0x08
	};
	hd44780fw_build_cc(cf, 1, cc);
	hd44780fw_write(cf, "\x01 wow \x01", 3, 1);
	_delay_ms(2000.0);
	
	char msg [] = "il faut eviter de trop manger...";
	uint8_t pos;
	
	hd44780fw_en_blink(cf, HD44780_L_DISP_B_ON);
	for (;;) {
		hd44780fw_clear(cf);
		for (pos = 0; pos < 32; ++pos) {
			hd44780fw_set_bc_index(cf, pos);
			hd44780fw_put(cf, msg[pos]);
			_delay_ms(250.0);
		}
		_delay_ms(2000.0);
	}
*/	
	return 0;
}
